package com.hengfoxwolf.jetpackdemo.ui.home
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.NavOptions
import androidx.navigation.fragment.findNavController
import com.hengfoxwolf.jetpackdemo.R
import com.hengfoxwolf.jetpackdemo.base.BaseFragment
import com.hengfoxwolf.jetpackdemo.constants.StringConstant
import com.hengfoxwolf.jetpackdemo.databinding.FragmentHome2Binding
import com.hengfoxwolf.jetpackdemo.databinding.FragmentHomeBinding

class HomeFragment : BaseFragment<FragmentHome2Binding>(FragmentHome2Binding::inflate),
    View.OnClickListener {

    private lateinit var homeViewModel: HomeViewModel

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        homeViewModel = ViewModelProvider(this)[HomeViewModel::class.java]

        mBinding.view1.setOnClickListener(this)
        mBinding.view2.setOnClickListener(this)
        mBinding.view3.setOnClickListener(this)
        mBinding.view4.setOnClickListener(this)
        mBinding.view5.setOnClickListener(this)
        mBinding.view6.setOnClickListener(this)
        mBinding.view7.setOnClickListener(this)
        mBinding.view8.setOnClickListener(this)
        mBinding.view9.setOnClickListener(this)
        mBinding.view10.setOnClickListener(this)

        Log.d(TAG, "value:${findContentFromJNI()}")

        val result = filterListFromJNI(mutableListOf(10, 3, 4, 19, 5, 6))
        Log.d(TAG, "result:$result")
    }

    override fun onClick(view: View?) {
        view?.apply {
            when (id) {
                R.id.view1 -> {
                    /*
                    * 此处通过navigate传递的Bundle数据  在目标Fragment中的arguments获取
                    * */
                    findNavController().navigate(R.id.nav_loading, Bundle().apply {
                        putInt(StringConstant.PARAM_VALUE, 145)
                    }, null)
                }
                R.id.view2 -> {
                    /*
                    * 通过NavOptions  控制返回当前界面还是退出应用
                    * setPopUpTo(destinationId: Int,inclusive: Boolean,saveState: Boolean = false)
                    * inclusive:true放进任务栈,否则不放
                    * setLaunchSingleTop(true)与NavOptions为null等效
                    * */
                    val navOptions = NavOptions.Builder().setPopUpTo(R.id.navigation_home, true).build()
                    findNavController().navigate(R.id.nav_loading, Bundle().apply {
                        putInt(StringConstant.PARAM_VALUE, 145)
                    }, navOptions)
                }
                R.id.view3 -> {
                    /*
                    * 获取目的地参数
                    * */
                    val arguments = findNavController().findDestination(R.id.nav_loading)?.arguments
                    arguments?.apply {
                        for ((_, value) in this) {
                            Log.d(TAG, "value:${value.defaultValue}")
                            Log.d(TAG, "value:${value.type}")
                            Log.d(TAG, "value:${value.isNullable}")
                            Log.d(TAG, "value:${value.isDefaultValuePresent}")
                        }
                    }
                }
                R.id.view4 -> {

                }
                R.id.view5 -> {

                }
                R.id.view6 -> {

                }
                R.id.view7 -> {

                }
                R.id.view8 -> {

                }
                R.id.view9 -> {

                }
                R.id.view10 -> {

                }
            }
        }
    }

    external fun findContentFromJNI(): String

    external fun filterListFromJNI(dataSet:MutableList<Int>): String

    companion object {
        init {
            System.loadLibrary("jetpackdemo")
        }

        const val TAG = "HomeFragment"
    }

}